#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , trie(){
    ui->setupUi(this);

    historyModel=new QStringListModel(this);
    ui->historyView->setModel(historyModel);
    ui->historyView->setVisible(false);

    connect(ui->searchEdit,&QLineEdit::textChanged,this,&MainWindow::onSearchTextChanged);
    connect(ui->historyView,&QListView::clicked,this,&MainWindow::onHistoryItemClicked);

    connect(ui->actionNew_Domain,&QAction::triggered,this,&MainWindow::openNewDomainDialog);
    connect(ui->actionSave_as,&QAction::triggered,this,&MainWindow::openSaveFileDialog);
    connect(ui->actionOpen_File,&QAction::triggered,this,&MainWindow::openOpenFileDialog);
}

MainWindow::~MainWindow(){
    delete ui;
}

void MainWindow::addSearchHistory(const std::string &word){
    trie.insert(word);
}

void MainWindow::openNewDomainDialog(){
    NewDomainDialog newDomainDialog(this);
    newDomainDialog.setModal(true);
    newDomainDialog.exec();
}

void MainWindow::openSaveFileDialog(){
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save as"),"",tr("Text Files (*.txt);;All Files (*)"));
    if(!fileName.isEmpty()){
        QFile file(fileName);
        if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
            QTextStream out(&file);
            DomainNameSystem::DNS().saveDomains(out);
            file.close();
            QMessageBox::information(this,tr("File Saved"),tr("File has been successfully saved at: ")+fileName);
        }
        else{
            QMessageBox::warning(this,tr("Error"),tr("Could not open file for writing."));
        }
    }
}

void MainWindow::openOpenFileDialog(){
    QString fileName=QFileDialog::getOpenFileName(this,tr("Open file"),"",tr("Text Files (*.txt);;All Files (*)"));
    if(!fileName.isEmpty()){
        QFile file(fileName);
        if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
            QTextStream in(&file);
            DomainNameSystem::DNS().readDomains(in);
            file.close();
            QMessageBox::information(this,tr("File opened"),tr("File has been successfully opened."));
        }
        else{
            QMessageBox::warning(this,tr("Error"),tr("Could not open file for reading."));
        }
    }
}

void MainWindow::onSearchTextChanged(const QString &text){
    std::vector<std::string> words=trie.getWordWithPrefix(text.toStdString());
    QStringList results;
    for(const auto &word:words)
        results.append(QString::fromStdString(word));
    historyModel->setStringList(results);
    ui->historyView->setVisible(!results.isEmpty());
}

void MainWindow::onHistoryItemClicked(const QModelIndex &index){
    QString selectedItem=historyModel->data(index).toString();
    ui->searchEdit->setText(selectedItem);
    ui->historyView->setVisible(false);
}

void MainWindow::on_searchButton_clicked(){
    QString searchText=ui->searchEdit->text();
    if(!searchText.isEmpty()){
        addSearchHistory(searchText.toStdString());
        qDebug()<<searchText;
    }
    ui->historyView->setVisible(false);
}

void MainWindow::on_debugButton_clicked(){
    DomainNameSystem::DNS().displayDomains();
}
